from wxpyview.ui.component.common.basewidget import *

class Panel(Widget):
    def _getParentParam(self, meta_widget):
        stopping_point = ['Tab', 'Frame', 'MDIChildFrame']       
        return ComponentLibrary.findFirstMatchedAncestor(meta_widget, stopping_point)

        # TODO: re-implement
        #return meta_widget.getParentMetaWidget().getWidget()

    def _setParams(self, meta_widget):
        dom = meta_widget.getDOM()
        if not dom:
            raise IOException
        
        self.params["parent"] = self._getParentParam(meta_widget)
        self.params["id"] = -1
        self.params["pos"] = ComponentLibrary.getPosParam(dom)
        self.params["size"] = ComponentLibrary.getSizeParam(dom)      
        #self.params["style"]
        self.params["name"] = dom.getAttribute("name")

    def _addStyleProperties(self, widget, meta_widget):
        dom = meta_widget.getDOM()
        
        bgcolor = dom.getAttribute("bgcolor")
        if bgcolor:
            widget.SetBackgroundColour(bgcolor)
        
    def create(self, meta_widget):
        self._setParams(meta_widget)
        
        if self.params["parent"].__class__.__name__ == "Tab":
            tabLabel = self.params["parent"].getLabel()
            tabGroup = self.params["parent"].getParent()
            self.params["parent"] = tabGroup
        
            panel = wx.Panel(**self.params)
            
            tabGroup.AddPage(panel, tabLabel)
            
            self._addStyleProperties(panel, meta_widget)
            return panel
        else:
            panel = wx.Panel(**self.params)
            self._addStyleProperties(panel, meta_widget)
            return panel